package com.han.bom.tests;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.han.bom.Answer;
import com.han.bom.MultipleChoice;
import com.han.bom.TrueOrFalse;

public class AnswerTest {

	private Answer answer = null;
	private MultipleChoice mc = null;
	private TrueOrFalse tf = null;
	
	@Before
	public void setUp() throws Exception {
		
		ArrayList<String> choices = new ArrayList<String>();
		
		for(int i=1;i<5;i++){
			choices.add("choice"+i);
		}
		
		tf = new TrueOrFalse();
		
		mc = new MultipleChoice(null,null,choices);
		
		answer = new Answer(null, null);
	}

	@After
	public void tearDown() throws Exception {
		tf = null;
		mc = null;
		answer = null;
	}

	@Test
	public void testIsCorrect() {
		
		//True or false tests
		tf.setAnswer("True");
		
		answer.setQuestion(tf);
		answer.setAnswer("True");
		assertEquals(true,answer.isCorrect());
		
		answer.setAnswer("False");
		assertEquals(false,answer.isCorrect());
		
		tf.setAnswer("False");
		assertEquals(true,answer.isCorrect());
		
		answer.setAnswer("");
		assertEquals(false,answer.isCorrect());
		
		answer.setAnswer(null);
		assertEquals(false,answer.isCorrect());
		
		//Multiple choice tests
		
		ArrayList<String> testAnswers = new ArrayList<String>();
		
		for(int i=1;i<5;i++){
			testAnswers.add("choice"+i);
		}
		testAnswers.add("");
		testAnswers.add(null);
		
		answer.setQuestion(mc);
		
		for(String ansText:testAnswers){
			
			answer.setAnswer(ansText);
			
			for(String availChoice: answer.getQuestion().getChoices()){
				
				mc.setAnswer(availChoice);
				
				if(availChoice.equals(ansText)){
					assertEquals(true,answer.isCorrect());
				}
				else{
					assertEquals(false,answer.isCorrect());
				}
				
			}
			
		}
		
	}

}
